package com.forlove.app.qselect.friend.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.forlove.app.R;
import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.common.widget.CircleImageView;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;
import com.forlove.app.qselect.index.ui.MineInfoActivity;
import com.forlove.app.qselect.login.entity.ContactsInfoEntity;

import java.util.ArrayList;
import java.util.List;


/**
 * 好友分组的adapter
 * Created by yjl on 2017/10/14.
 */

public class FriendGroupAdapter extends BaseExpandableListAdapter {
    private ArrayList<String> groupList = new ArrayList<>();
    private Context mContext;
    private ArrayList<MineFriendEntity> friendEntities = new ArrayList<>();
    private IBasePresenter iBasePresenter;
    private List<List<MineFriendEntity>> childFriendEntity = new ArrayList<>();
    private List<ContactsInfoEntity> contactsInfoEntities = new ArrayList<>();

    public FriendGroupAdapter(Context context, IBasePresenter presenter, List<MineFriendEntity> friendEntities) {
        this.mContext = context;
        this.friendEntities.clear();
        this.friendEntities.addAll(friendEntities);
        if (friendEntities.size() > 0) {
            getContacts();
        }
        groupList.clear();
        groupList.addAll(initGroup());
        initChildFriend();
        this.iBasePresenter = presenter;
    }

    public void setContacts(List<ContactsInfoEntity> contactsInfoEntities) {
        this.contactsInfoEntities.clear();
        this.contactsInfoEntities.addAll(contactsInfoEntities);
    }

    public void updateFriendList(List<MineFriendEntity> friendEntities) {
        this.friendEntities.clear();
        this.friendEntities.addAll(friendEntities);
        if (this.friendEntities.size() > 0) {
            getContacts();
        }
        groupList.clear();
        groupList.addAll(initGroup());
        initChildFriend();
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        return childFriendEntity.get(groupPosition).size();
    }

    @Override
    public String getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public MineFriendEntity getChild(int groupPosition, int childPosition) {
        return childFriendEntity.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {

        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        String title = getGroup(groupPosition);
        MineFriendEntity friendEntity = friendEntities.get(childPosition);
        if (title.equalsIgnoreCase(friendEntity.realname_fc)) {
            return childPosition;
        }
        return 0;
    }


    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View
            convertView, ViewGroup parent) {
        FriendGoupViewHolder friendGoupViewHolder;
        if (convertView == null) {
            LayoutInflater inflater =
                    (LayoutInflater) mContext.getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_friend_group, null);
            friendGoupViewHolder = new FriendGoupViewHolder();
            friendGoupViewHolder.goup = (TextView) convertView.findViewById(R.id.text_friend_group);
            convertView.setTag(friendGoupViewHolder);
        } else {
            friendGoupViewHolder = (FriendGoupViewHolder) convertView.getTag();
        }
        if (friendGoupViewHolder != null) {
            friendGoupViewHolder.goup.setText(groupList.get(groupPosition));

        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View
            convertView, ViewGroup parent) {
        FriendViewHolder friendViewHolder = null;
        if (convertView == null) {
            LayoutInflater inflater =
                    (LayoutInflater) mContext.getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_mine_friend, null);
            friendViewHolder = new FriendViewHolder();
            friendViewHolder.nickeName = (TextView) convertView.findViewById(R.id.text_friend_nickname);
            friendViewHolder.friendFrom = (TextView) convertView.findViewById(R.id.text_friend_from);
            friendViewHolder.friendName = (TextView) convertView.findViewById(R.id.text_friend_name);
            friendViewHolder.friendImage = (CircleImageView) convertView.findViewById(R.id.view_friend_photo);
            convertView.setTag(friendViewHolder);
        } else {
            friendViewHolder = (FriendViewHolder) convertView.getTag();
        }

        if (friendViewHolder != null) {
            final MineFriendEntity friendEntity = getChild(groupPosition, childPosition);
            if (friendEntity != null) {
                friendViewHolder.nickeName.setText(friendEntity.realname);
                iBasePresenter.loadImageView(mContext, friendViewHolder.friendImage,
                        friendEntity.img, R.mipmap.image_mine_friend_avatar);
                if (friendEntity.isFromContacts) {
                    friendViewHolder.friendFrom.setText("手机联系人:");
                    friendViewHolder.friendName.setText(friendEntity.contactsName);
                    friendViewHolder.friendName.setVisibility(View.VISIBLE);
                } else if (StringUtils.isNotEmpty(friendEntity.mConcatName)) {
                    friendViewHolder.friendFrom.setText("手机联系人:");
                    friendViewHolder.friendName.setText(friendEntity.mConcatName);
                    friendViewHolder.friendName.setVisibility(View.VISIBLE);
                } else if (StringUtils.isNotEmpty(friendEntity.username)) {
                    friendViewHolder.friendFrom.setText("圈一圈:");
                    friendViewHolder.friendName.setText(friendEntity.username);
                    friendViewHolder.friendName.setVisibility(View.VISIBLE);
                } else {
                    if (StringUtils.isNotEmpty(friendEntity.realname)) {
                        friendViewHolder.friendFrom.setText("圈一圈好友");
                        friendViewHolder.friendName.setVisibility(View.INVISIBLE);
                    } else if (StringUtils.isNotEmpty(friendEntity.mConcatName)) {
                        friendViewHolder.friendFrom.setText("通讯录好友");
                        friendViewHolder.friendName.setVisibility(View.INVISIBLE);
                    }
                }
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, MineInfoActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("mobile", friendEntity.mobile);
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);
                    }
                });
            }
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    private void getContacts() {

        for (int i = 0; i < friendEntities.size(); i++) {
            MineFriendEntity friendEntity = friendEntities.get(i);
            for (int k = 0; k < contactsInfoEntities.size(); k++) {
                ContactsInfoEntity contactsInfoEntity = contactsInfoEntities.get(k);
                if (!friendEntity.realname_fc.equalsIgnoreCase(contactsInfoEntity.getSortKey())) {
                    continue;
                }
                if (contactsInfoEntity.getNumber().equalsIgnoreCase(friendEntity.mobile)) {
                    friendEntity.contactsName = contactsInfoEntity.getName();
                    friendEntity.isFromContacts = true;
                }

            }
        }
    }

    /**
     * 初始化小组数据
     *
     * @return
     */
    private List<String> initGroup() {
        List<String> groupList = new ArrayList<>();
        for (MineFriendEntity friendEntity : friendEntities) {
            String group = friendEntity.realname_fc.toUpperCase();
            if (!groupList.contains(group)) {
                groupList.add(group);
            }
        }
        return groupList;
    }

    /**
     * 分组联系人数据
     *
     * @param groupPos
     * @return
     */
    private List<MineFriendEntity> getChildFriend(int groupPos) {
        List<MineFriendEntity> entities = new ArrayList<>();
        String str = groupList.get(groupPos);
        for (MineFriendEntity friend : friendEntities) {
            if (str.equalsIgnoreCase(friend.realname_fc)) {
                entities.add(friend);
            }
        }
        return entities;

    }

    private void initChildFriend() {
        childFriendEntity.clear();
        for (int i = 0; i < groupList.size(); i++) {
            List<MineFriendEntity> entities = getChildFriend(i);
            childFriendEntity.add(entities);
        }
    }

    /**
     * 分组viewholder
     */
    public class FriendGoupViewHolder {
        public TextView goup;
    }

    public class FriendViewHolder {
        public TextView nickeName; //别名
        public TextView friendFrom;//从哪来
        public TextView friendName;//姓名
        public CircleImageView friendImage;//好友头像
    }
}
